package cn.jiedh.app.meiweidk.web.webhandle;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.GsonBuilder;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.L;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.base.BaseFragment;
import cn.jiedh.app.meiweidk.login.LoginActivity;
import cn.jiedh.app.meiweidk.network.ResponseFailure;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.response.BaseResponse;
import cn.jiedh.app.meiweidk.web.webkit.WJBridgeHandler;
import cn.jiedh.app.meiweidk.web.webkit.WJCallbacks;

import org.xutils.ex.HttpException;

/**
 * @name：
 * @author： liuyun
 * @phone： 15723310658
 * @createTime： 2017/12/14
 * @modifyTime： 2017/12/14
 * @explain：说明
 */


public class TranslateDataHandle implements WJBridgeHandler {
    public static final String HANDLE_NAME="translateData";
    private Context mCox;
    private Fragment mFragment;

    public TranslateDataHandle(Context mCox) {
        this.mCox = mCox;
    }

    public TranslateDataHandle(Fragment mFragment) {
        this.mFragment = mFragment;
    }

    @Override
    public void handler(String data, WJCallbacks callbacks) {
        if(L.isDebug) {
            System.out.println("TranslateDataHandle-----------------------------------------------------------------------------");
            System.out.println("data-->" + data);
        }
        TranslateDataHandleBean bean=null;
        try {
            bean= new GsonBuilder().create().fromJson(data,TranslateDataHandleBean.class);
        }catch (Exception e){
            if(L.isDebug){
                L.e("解析异常");
                e.printStackTrace();
            }
            return;
        }

        if(bean.isNeedToken() && !SPUtils.getIsLogin()){//跳转登录
            Bundle bundle = new Bundle();
            bundle.putBoolean("isGoHome",true);
            ActivityUtil.next(mCox!=null?mCox:mFragment.getActivity(), LoginActivity.class,bundle);
        }else{//请求接口
            if(mFragment!=null && mFragment instanceof BaseFragment){
                ((BaseFragment) mFragment).request(bean.getApiUrl(), bean.getData(), bean.isEncrypt(), new ResponseSuccess<JSONObject>() {
                    @Override
                    public void onRequestSucceed(BaseResponse<JSONObject> response) {
                        callbacks.onCallback(response.getData().toJSONString());
                    }
                }, new ResponseFailure() {
                    @Override
                    public void onRequestFailure(HttpException error, String msg) {
                        T.showLong(msg);
                        callbacks.onCallback(new GsonBuilder().create().toJson(new BaseResponse<Object>(-10000)));
                    }
                });
            }else if(mCox!=null && mCox instanceof BaseActivity){
                 BaseActivity activity= (BaseActivity) mCox;
                activity.request(activity,bean.getApiUrl(), bean.getData(), bean.isEncrypt(), new ResponseSuccess<JSONObject>() {
                    @Override
                    public void onRequestSucceed(BaseResponse<JSONObject> response) {
//                        System.out.println("onCallback-----"+new GsonBuilder().create().toJson(response));
                         //请求数据返给
                        callbacks.onCallback(new GsonBuilder().create().toJson(response));
                    }
                }, new ResponseFailure() {
                    @Override
                    public void onRequestFailure(HttpException error, String msg) {
                        T.showLong(msg);
                        callbacks.onCallback(new GsonBuilder().create().toJson(new BaseResponse<Object>(-10000)));
                    }
                });
            }
        }
    }
}
